package com.service;

import com.dao.goodsdao;
import com.dao.saledao;
import com.domain.goods;
import com.domain.sale;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class buyImpl implements buy {
    @Autowired
    saledao saledao;
    @Autowired
    goodsdao goodsdao;

    @Override
    public void buy(int id, int nums) {
        goods goods=goodsdao.select(id);
        saledao.insert(new sale(0,id,
                nums));
        goodsdao.update(new goods(id,null,nums,0));
        if (goods==null){
                throw new NullPointerException("没有该商品！！");
        }
        else if (goods.getAmount()==0) {

                throw new NullPointerException("商品已售罄！！");
        }
        else if (goods.getAmount()-nums<0) {
                throw new NullPointerException("商品库存不足！！");
        }
        else
        System.out.println("您买了"+nums+"个"+goods.getName()+
                "共花费"+goods.getPrice()*nums+"元");
    }
    public void find() {
        List<goods> list = goodsdao.selectall();
        for (goods g : list
        ) {
            System.out.println(g);
        }
    }
}
